package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.BrowsingHistoryMapper;
import com.guigu.pojo.BrowsingHistory;
import com.guigu.service.BrowsingHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class BrowsingHistoryServiceImpl extends ServiceImpl<BrowsingHistoryMapper,BrowsingHistory>
        implements BrowsingHistoryService {

    @Autowired
    BrowsingHistoryService browsingHistoryService;

    @Autowired
    BrowsingHistoryMapper browsingHistoryMapper;

    //根据用户id和商品id查询浏览记录
    //如果记录存在 修改 否则 添加
    @Override
    public Map createbrowsinghistory(BrowsingHistory browsingHistory) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","失败");
        QueryWrapper<BrowsingHistory> queryWrapper = new QueryWrapper<BrowsingHistory>();
        if (browsingHistory.getPId()!=null){
            queryWrapper.eq("p_id",browsingHistory.getPId());
        }
        if (browsingHistory.getGId()!=null){
            queryWrapper.eq("g_id",browsingHistory.getGId());
        }
        BrowsingHistory browsingHisy=browsingHistoryMapper.selectOne(queryWrapper);
        if (browsingHisy!=null){
            browsingHistoryService.editebrowsingHistory(browsingHisy);
            map.put("code",0);
            map.put("msg","修改成功");
        }else {
            browsingHistoryService.addbrowsingHistory(browsingHistory);
            map.put("code",0);
            map.put("msg","添加成功");
        }
        return map;
    }

    //添加浏览记录
    @Override
    public Map addbrowsingHistory(BrowsingHistory browsingHistory) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "添加失败");
        browsingHistory.setBrowsingDate(new Date());
        boolean b=this.save(browsingHistory);
        if (b){
            map.put("code", 1);
            map.put("msg", "添加成功");
        }
        return map;
    }

    //修改浏览记录
    @Override
    public Map editebrowsingHistory(BrowsingHistory browsingHistory) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "修改失败");
        browsingHistory.setBrowsingDate(new Date());
        boolean b=this.updateById(browsingHistory);
        if (b){
            map.put("code", 1);
            map.put("msg", "修改成功");
        }
        return map;
    }
}
